﻿    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Net;  // For HttpWebRequest and HttpWebResponse classes.
    using System.Security.Cryptography.X509Certificates;  // For certificate-related classes.
    using System.IO;  // For Stream classes.

//ERM . sourced from http://msdn.microsoft.com/en-us/library/windowsazure/gg651127.aspx 12112011:1148

namespace Clarendon.AzureBilling.APIInvestigationApp
{


  class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    // X.509 certificate variables.
                    X509Store certStore = null;
                    X509Certificate2Collection certCollection = null;
                    X509Certificate2 certificate = null;

                    // Request and response variables.
                    HttpWebRequest httpWebRequest = null;
                    HttpWebResponse httpWebResponse = null;

                    // Stream variables.
                    Stream responseStream = null;
                    StreamReader reader = null;

                    // URI variable.
                    Uri requestUri = null;

                    // Specify operation to use for the service management call.
                    // This sample will use the operation for listing the hosted services.
                    string operation = "hostedservices";

                    // The ID for the Windows Azure subscription.
                    string subscriptionId = "25ddb5a3-15b3-45f9-bf64-c0a625c16898";

                    // The thumbprint for the certificate. This certificate would have been
                    // previously added as a management certificate within the Windows Azure management portal.
                    string thumbPrint = "8F746262CC0B0183838DF8FDF0943C9778981B28";

                    // Open the certificate store for the current user.
                    certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                    certStore.Open(OpenFlags.ReadOnly);

                    // Find the certificate with the specified thumbprint.
                    certCollection = certStore.Certificates.Find(
                                         X509FindType.FindByThumbprint,
                                         thumbPrint,
                                         false);

                    // Close the certificate store.
                    certStore.Close();

                    // Check to see if a matching certificate was found.
                    if (0 == certCollection.Count)
                    {
                        throw new Exception("No certificate found containing thumbprint " + thumbPrint);
                    }

                    // A matching certificate was found.
                    certificate = certCollection[0];
                    Console.WriteLine("Using certificate with thumbprint: " + thumbPrint);

                    // Create the request.
                    requestUri = new Uri("https://management.core.windows.net/"
                                         + subscriptionId
                                         + "/services/"
                                         + operation);

                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(requestUri);

                    // Add the certificate to the request.
                    httpWebRequest.ClientCertificates.Add(certificate);

                    // Specify the version information in the header.
                    httpWebRequest.Headers.Add("x-ms-version", "2011-02-25");

                    // Make the call using the web request.
                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                    // Display the web response status code.
                    Console.WriteLine("Response status code: " + httpWebResponse.StatusCode);

                    // Display the request ID returned by Windows Azure.
                    if (null != httpWebResponse.Headers)
                    {
                        Console.WriteLine("x-ms-request-id: "
                        + httpWebResponse.Headers["x-ms-request-id"]);
                    }

                    // Parse the web response.
                    responseStream = httpWebResponse.GetResponseStream();
                    reader = new StreamReader(responseStream);
                    // Display the raw response.
                    Console.WriteLine("Response output:");
                    Console.WriteLine(reader.ReadToEnd());

                    // Close the resources no longer needed.
                    httpWebResponse.Close();
                    responseStream.Close();
                    reader.Close();
                }
                catch (Exception e)
                {

                    Console.WriteLine("Error encountered: " + e.Message);

                    // Exit the application with exit code 1.
                    System.Environment.Exit(1);

                }
                finally
                {
                    Console.ReadKey();
                    // Exit the application.
                    System.Environment.Exit(0);
                }

            }
        }
    }


